package com.zx.link.linstener;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.zx.link.entity.LinkType;
import com.zx.link.service.LinkTypeService;

/**
 * 系统初始化，加载器
 * @author zhangx
 */
@Component
@Scope("singleton")
public class SystemResources implements ServletContextAware {
	
	/****静态资源****/
	public static byte[] DEFAUTL_IMG_BIN = null;
	
	public static List<LinkType> LINK_TYPES = null;
	
	
	/****业务实例****/
	@Autowired
	private LinkTypeService linkTypeService ;
	
	/**
	 * 核心方法
	 */
	public void setServletContext(ServletContext app) {
		initImg(app);
		getTypes();
	}
	
	/**
	 * 初始化图片
	 * @param app
	 */
	private void initImg(ServletContext app){
		InputStream is = null;
		try {
			String defaultImgPath = app.getRealPath("/static/img/default-64x64.jpg");
			System.out.println(defaultImgPath);
			is = new FileInputStream(defaultImgPath);
			DEFAUTL_IMG_BIN = new byte[is.available()];
			is.read(DEFAUTL_IMG_BIN);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void getTypes(){
		LINK_TYPES = this.linkTypeService.getAll();
	}
	
}
